//
//  LoggingLevelView.m
//  BangoiOSSDKTestApp
//
//  Created by Development Bango on 12/06/2013.
//  Copyright (c) 2013 Development Bango. All rights reserved.
//

#import "LoggingLevelView.h"

#define STATUS_BAR_HEIGHT 40

@implementation LoggingLevelView

@synthesize buttonCancel      = _buttonCancel;
@synthesize buttonDone        = _buttonDone;
@synthesize contentView       = _contentView;
@synthesize tableLoggingLevel = _tableLoggingLevel;
@synthesize selectedEvents    = _selectedEvents;

#pragma mark -
#pragma mark Object Lifecycle

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self)
    {
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}



#pragma mark -
#pragma mark View Lifecycle

- (void)loadView
{
    _contentView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
	_contentView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
	self.view = _contentView;
    
    _buttonDone = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    _buttonDone.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
    [_buttonDone setTitle:@"Done" forState:UIControlStateNormal];
    _buttonDone.frame = CGRectMake(_contentView.frame.size.width-100, 0, 100, 40);
    [_buttonDone addTarget:self action:@selector(buttonDonePressed:) forControlEvents:UIControlEventTouchUpInside];
    [_contentView addSubview:_buttonDone];
    
    _buttonCancel = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    _buttonCancel.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
    [_buttonCancel setTitle:@"Cancel" forState:UIControlStateNormal];
    _buttonCancel.frame = CGRectMake(0, 0, 100, 40);
    [_buttonCancel addTarget:self action:@selector(buttonCancelPressed:) forControlEvents:UIControlEventTouchUpInside];
    [_contentView addSubview:_buttonCancel];
    
    _tableLoggingLevel = [[UITableView alloc]initWithFrame:CGRectMake(0, STATUS_BAR_HEIGHT, _contentView.frame.size.width, _contentView.frame.size.height - STATUS_BAR_HEIGHT) style:UITableViewStylePlain];
    _tableLoggingLevel.dataSource = self;
    _tableLoggingLevel.delegate = self;
    _tableLoggingLevel.autoresizingMask= (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
    [_contentView addSubview:_tableLoggingLevel];
    
    _analytics = [BGOAnalyticsManager sharedManager];
    
    _selectedEvents = [[NSMutableDictionary alloc] init];
    
    [self setCheckmarks];
    [_tableLoggingLevel reloadData];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark -
#pragma mark Logging Level

-(void)setCheckmarks
{
    int logLevel = [[BGOAnalyticsManager sharedManager] loggingLevel];
    
    _checkmarks[ 0 ] =  (BOOL)((logLevel & (int)LEVEL_VIDEO)               == (int)LEVEL_VIDEO);
    _checkmarks[ 1 ] =  (BOOL)((logLevel & (int)LEVEL_AUDIO)               == (int)LEVEL_AUDIO);
    _checkmarks[ 2 ] =  (BOOL)((logLevel & (int)LEVEL_PAGEVIEW_AND_SEARCH) == (int)LEVEL_PAGEVIEW_AND_SEARCH);
}

#pragma mark -
#pragma mark IBActions

-(IBAction)buttonCancelPressed:(id)sender
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

-(IBAction)buttonDonePressed:(id)sender
{
    int level = 0;
    
    if (_checkmarks[ 0 ] )
    {
        level |= (int)LEVEL_VIDEO;
    }
    
    if (_checkmarks[ 1 ] )
    {
        level |= (int)LEVEL_AUDIO;
    }
    
    if (_checkmarks[ 2 ] )
    {
        level |= (int)LEVEL_PAGEVIEW_AND_SEARCH;
    }
    
    [[BGOAnalyticsManager sharedManager] setLoggingLevel:level];
    
    [self dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark -
#pragma mark UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return sizeof(_checkmarks) / sizeof(BOOL);
}

- (UITableViewCell *)tableView:(UITableView *)myTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
	NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [myTableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }
    
    switch (1<<indexPath.row)
    {
        case LEVEL_AUDIO:
        {
            cell.textLabel.text = @"Log Audio Events Only";
            break;
        }
        case LEVEL_PAGEVIEW_AND_SEARCH:
        {
            cell.textLabel.text = @"Log PageView/Search Events Only";
            break;
        }
        case LEVEL_VIDEO:
        {
            cell.textLabel.text = @"Log Video Events Only";
            break;
        }
    }
    
    if (_checkmarks[indexPath.row])
        [cell setAccessoryType:UITableViewCellAccessoryCheckmark];
    else
        [cell setAccessoryType:UITableViewCellAccessoryNone];
    
    return cell;
}

#pragma mark -
#pragma mark UITableViewDelegate

// Override to support row selection in the table view.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    _checkmarks[ indexPath.row ] = !_checkmarks[ indexPath.row ];
    [tableView reloadData];
}

@end
